package net.googlecode.kharchenko.validation;

import net.googlecode.kharchenko.domains.Person;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

/**
 * Class for validating fields of Person object.
 */
@Component("registrationValidator")
public class RegistrationValidation {

    /**
     * Check if passwords are equals.
     *
     * @param pass        password
     * @param confirmpass confirm password
     * @param errors      errors
     */
    public void validateConfirm(String pass, String confirmpass, Errors errors) {

        if (!pass.equals(confirmpass)) {
            errors.rejectValue("password", "confirm.notequal");
        }
    }

    /**
     * Validate First name of Person.
     *
     * @param prs    person to be validated
     * @param errors errors
     */


    public void validateFname(Person prs, Errors errors) {

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fname",
                "NotEmpty.registration.fName");
        String fName = prs.getFname();
        if ((fName.length()) > 50) {
            errors.rejectValue("fname",
                    "lengthOfUser.registration.fName");
        }

    }

    /**
     * Validate name of image. Must be .jpg or .png.
     *
     * @param name   name of image
     * @param size   size of file
     * @param errors errors
     * @return extension of file.
     */
    public String validateImageUrl(String name, Long size, Errors errors) {
        if (name.equals("")) return "Empty";
        int dotPos = name.lastIndexOf(".");
        String ext = name.substring(dotPos);
        if ((!ext.toLowerCase().equals(".jpg")) && (!ext.toLowerCase().equals(".png")))
            errors.rejectValue("imgurl", "badType.img");
        if (size > 400000) {
            errors.rejectValue("imgurl", "BadSize.img");
        }
        return ext;
    }

    /**
     * Validate Second name of Person.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validateSname(Person prs, Errors errors) {


        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sname",
                "NotEmpty.registration.sName");
        String sName = prs.getSname();
        if ((sName.length()) > 50) {
            errors.rejectValue("sname",
                    "lengthOfUser.registration.sName");
        }
    }

    /**
     * Validate password of Person.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validatePassword(Person prs, Errors errors) {


        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
                "NotEmpty.registration.password");
        String pass = prs.getPassword();
        if ((pass.length()) > 50) {
            errors.rejectValue("password",
                    "lengthOfUser.registration.password");
        }
    }

    /**
     * Validate patronymic of Person.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validateTname(Person prs, Errors errors) {

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tname",
                "NotEmpty.registration.tName");
        String tName = prs.getTname();
        if ((tName.length()) > 50) {
            errors.rejectValue("tname",
                    "lengthOfUser.registration.tName");
        }
    }

    /**
     * Validate e-mail of Person.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validateEmail(Person prs, Errors errors) {

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
                "NotEmpty.registration.email");
        if (prs.getEmail().length() == 0) return;
        int atpos = prs.getEmail().indexOf("@");
        int dotpos = prs.getEmail().lastIndexOf(".");
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= prs.getEmail().length()) {
            errors.rejectValue("email",
                    "Invalid.registration.email");
        }

    }

    /**
     * Validate fields of register form.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validateRegForm(Person prs, Errors errors) {

        validateFname(prs, errors);
        validateSname(prs, errors);
        validateTname(prs, errors);
        validateEmail(prs, errors);
        validatePassword(prs, errors);

    }

    /**
     * Validate fields of changeinfo form.
     *
     * @param prs    person to be validated
     * @param errors errors
     */
    public void validateChangeInfoForm(Person prs, Errors errors) {

        validateFname(prs, errors);
        validateSname(prs, errors);
        validateTname(prs, errors);

    }

}
